#include <stdio.h>

// 二分查找函数:必须是顺序表
int binary_search(int arr[], int n, int target) 
{
    int left = 0, right = n - 1;
    while (left <= right) 
    {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target)
         {
            return mid;
        } else if (arr[mid] < target)
         {
            left = mid + 1;
        } else
         {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 7, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 7;
    int result = binary_search(arr, n, target);
    if (result != -1) {
        printf("目标元素 %d 在数组中的索引是 %d\n", target, result);
    } else {
        printf("目标元素 %d 不在数组中\n", target);
    }
    return 0;
}    